#include "Client.hpp"
#include <memory>
#include <string>
#include <cstdlib>

using namespace tcp_cli;


static void usage(const char* port)
{
    std::cout<<"USAGE:\n\t"<<port<<" serverip serverport\n"<<std::endl;
}

enum
{
    LEFT,
    OPER,
    RIGHT
};

// 10+20
Request ParseLine(const std::string &line)
{
    std::string left, right;
    char op;
    int status = LEFT;

    int oper = 1; //正负数

    int i = 0;
    if(line[i] == '-') 
    {
        oper *= -1;
        ++i;
    }
    else if(line[i] == '+') ++i;

    while(i < line.size())
    {

        switch (status)
        {
        case LEFT:
            if (isdigit(line[i]))
                left.push_back(line[i++]);
            else
                status = OPER;
            break;
        case OPER:
            op = line[i++];
            status = RIGHT;
            break;
        case RIGHT:
            if (isdigit(line[i]))
                right.push_back(line[i++]);
            break;
        default :i++;
        }
    }

    Request req;
    // std::cout << "left: " << left << std::endl;
    // std::cout << "right: " << right << std::endl;
    // std::cout << "op: " << op << std::endl;

    req._x = oper*std::stoi(left);
    req._y = std::stoi(right);
    req._op = op;

    return req;
}



int main(int argc,char* argv[])
{
    if(argc != 3)
    {
        usage(argv[0]);
        exit(USAGE_ERR);
    }
    const std::string ip = argv[1];
    const uint16_t port = atoi(argv[2]);

    std::unique_ptr<Client> cli(new Client(ParseLine,ip,port));
    cli->InitClient();
    cli->Start();
    
    return 0;
}

